<template>
  <div class="app-container">
    <div v-show="!open">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="80px"
      >
        <el-form-item label="开始时间" prop="sessionStartTime">
          <div class="block">
            <el-date-picker
              v-model="queryParams.sessionStartTime"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="结束时间" prop="sessionEndTime">
          <div class="block">
            <el-date-picker
              v-model="queryParams.sessionEndTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <div style=" border: 1px solid #E4E7ED; width='100%'">
        <el-table
          v-loading="loading"
          border
          :data="addressList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="考试名称"
            align="center"
            :show-overflow-tooltip="true"
            prop="name"
          />
          <!-- <el-table-column
          label="场次"
          align="center"
          :show-overflow-tooltip="true"
          prop="sessionName"
        /> -->
          <el-table-column
            label="开始时间"
            align="center"
            :show-overflow-tooltip="true"
            prop="sessionStartTime"
          />
          <el-table-column
            label="结束时间"
            align="center"
            :show-overflow-tooltip="true"
            prop="sessionEndTime"
          />
          <!-- <el-table-column
          label="试卷"
          align="center"
          :show-overflow-tooltip="true"
          prop="paperName"
          width="600"
        /> -->
          <el-table-column
            label="操作"
            align="center"
            :show-overflow-tooltip="true"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">

              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                round
                @click="handleUpdate(scope.row)"
                >选择该考试</el-button
              >
              <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >进入考试</el-button
            > -->
            </template>
          </el-table-column>
        </el-table>
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改考场管理对话框 -->
    <div v-show="open">
      <!-- <el-dialog :title="title" :visible.sync="open"  width="90%" append-to-body> -->
      <exam v-if="open" :row="row" @ok="ok" ></exam>
      <!-- </el-dialog> -->
    </div>
  </div>
</template>

<script>
import { listExamiton } from "@/api/exam/review";
import exam from "./exam.vue";
export default {
  name: "Review",
  components: { exam },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考场管理表格数据
      addressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sessionStartTime: "",
        sessionEndTime: "",
      },
      //组件通信
      row: [],
      // 表单参数
      form: {},
      // 表单校验
      // 表单校验
      rules: {
        name: [
          { required: true, message: "场地名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //子组件通信，触发父子间的事件
    ok(message) {
      // this.open=false
    },
    /** 查询考场管理列表 */
    getList() {
      this.loading = true;
      listExamiton(this.queryParams).then((response) => {
        this.addressList = response.rows;
        if (this.addressList != null) {
          for (let i = 0; i < this.addressList.length; i++) {
            this.countDown(this.addressList[i]);
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },

    //处理时间
    countDown(templatedata) {
      let now = new Date(); // 获取当前时间
      if (Date.parse(templatedata.sessionEndTime) < Date.parse(now)) {
        templatedata.attend = false;
      } else {
        templatedata.attend = true;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        capacity: null,
        address: null,
        state: null,
        addressExplain: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考场管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids
      // getAddress(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改考场管理";
      // });
      this.row = row;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAddress(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAddress(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除考场管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAddress(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有考场管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportAddress(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
</style>
